package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.PlatformNoticeData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerPlatformNoticeComponent;
import com.eduhzy.ttw.teacher.mvp.contract.PlatformNoticeContract;
import com.eduhzy.ttw.teacher.mvp.presenter.PlatformNoticePresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

;

/**
 * @author xubo
 * @date 2018-7-20 16:41:22
 */
@Route(path = RouterHub.APP_PLATFORMNOTICEACTIVITY)
public class PlatformNoticeActivity extends BaseActivity<PlatformNoticePresenter> implements PlatformNoticeContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @Inject
    BaseQuickAdapter<PlatformNoticeData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager layoutManager;
    @Inject
    List<PlatformNoticeData> mList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        setTitle("系统通知");
        //如找不到该类,请编译一下项目
        DaggerPlatformNoticeComponent
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.activity_platform_notice;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> {
            getData(true);
        });
        getData(true);
        mAdapter.setOnLoadMoreListener(() -> getData(false), rvList);
//        mAdapter.setOnItemClickListener((adapter, view, position) -> {
//            PlatformNoticeData noticeData = mAdapter.getData().get(position);
//            if (noticeData.getOpenType().equals("local")) {
//                ARouter.getInstance().build(RouterHub.APP_NOTICEDETAILACTIVITY)
//                        .withString(Intent.EXTRA_TITLE, Constants.NOTICE_DETAIL)
//                        .withParcelable(Constants.PARCELABLE_DATA, noticeData)
//                        .navigation(getActivity());
//            } else {
//                ARouter.getInstance().build(RouterHub.APP_WEBVIEWACTIVITY)
//                        .withString(Intent.EXTRA_TITLE, Constants.NOTICE_DETAIL)
//                        .withString(Constants.ROUTER_STRING, noticeData.getLinkUrl())
//                        .navigation(getActivity());
//            }
//            noticeData.setHasRead(1);
//            mAdapter.notifyItemChanged(position);
//            mPresenter.markRead(noticeData.getInboxId());
//
//
//        });
    }

    private void getData(boolean pullToRefresh) {
        mPresenter.getSendMsgList(pullToRefresh, 500);
    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
